class EPad extends BaseImage{
    type = "pad";

    data = {
        b: null,
        bs_t: null,
        bs_b: null,
        bp_t: null,
        bp_b: null,
        t: null,
        ts_t: null,
        ts_b: null,
        tp_t: null,
        tp_b: null,
        i: null,
        is_t: null,
        is_b: null,
        ip_t: null,
        ip_b: null,
        drill: []
    }

    kind = "smt"

    constructor(units,data){
        super(units);
        if(data){
            this.setRawData(data);
        }
    }

    getRawData(){
        let padOutLayer = ["b","t","i"];
        let padInLayers = {
            b:["b","bs_t","bs_b","bp_t","bp_b"],
            t:["t","ts_t","ts_b","tp_t","tp_b"],
            i:["i","is_t","is_b","ip_t","ip_b"]
        };
        let layerFormTrans = {C:"CIRCLE",S:"RECTANGLE",OC:"OCTAGON",OV:"OBLONG"};
        let drillFormTrans = {C:"CIRCLE_DRILL",S:"RECTANGLE_SLOT",OC:"OCTAGON_DRILL",OV:"OVAL_SLOT"};

        let pad = {
            id: this.id,
            kind: this.kind,
            drill:[]
        };

        padOutLayer.forEach(oLayer=>{
            pad[oLayer] = {};
            padInLayers[oLayer].forEach(mLayer=>{
                if(this.data[mLayer]){
                    let layerData = this.data[mLayer].figure.getRawData();
                    layerData.skewing_x = this.data[mLayer].offset.x;
                    layerData.skewing_y = this.data[mLayer].offset.y;
                    layerData.rotate = this.data[mLayer].rotate;
                    layerData.form = layerFormTrans[layerData.form] || layerData.form;
                    pad[oLayer][mLayer] = layerData;
                }else{
                    pad[oLayer][mLayer] = {};
                }
            });
        });

        this.data.drill.forEach(drillIns=>{
            let drill = drillIns.figure.getRawData();
                drill.skewing_x = drillIns.offset.x;
                drill.skewing_y = drillIns.offset.y;
                drill.rotate = drillIns.rotate;

            let originDrill = {
                hole: {
                    rotate: drill.rotate,
                    form: drillFormTrans[drill.form] || drill.form,
                    w: drill.w,
                    h: drill.h
                },
                keepout: "nil",
                figure: {
                    w: drill.w,
                    name: "CROSS",
                    h: drill.h
                },
                diameter: drill.w,
                offset: {
                    x: drill.skewing_x,
                    y: drill.skewing_y
                },
                plate: "nil",
                tolerance: {
                    x: 0,
                    y: 0
                },
                uvia: "nil",
                start_end_layer: {
                    end_layer: "ETCH/BOTTOM",
                    st_layer: "ETCH/TOP"
                }
            }
            if(drill.form == "C"){
                originDrill.figure.name = "Null";
            }else if(drill.form == "OV"){
                originDrill.figure.name = "OblongX";
            }else if(drill.form == "S"){
                originDrill.figure.name = "Rectangle";
            }
            pad.drill.push(originDrill);
        });
        return pad;
    }

    setRawData(data){
        super.setRawData(data);

        this.kind = data.kind;

        let self = this;

        let padBigLayer = ["b","t","i","drill"];
        let padSmallLayer = {b:["b","bs_t","bs_b","bp_t","bp_b"],t:["t","ts_t","ts_b","tp_t","tp_b"],i:["i","is_t","is_b","ip_t","ip_b"]};

        padBigLayer.forEach(bigLayer=>{
            if(bigLayer !== "drill"){
                padSmallLayer[bigLayer].forEach(sLayer=>{
                    if(data[bigLayer][sLayer] && data[bigLayer][sLayer].form){
                        this.data[sLayer] = createLayerData(data[bigLayer][sLayer]);
                    }
                })
            }else{
                if(data[bigLayer] instanceof Array){
                    data[bigLayer].forEach(layerData=>{
                        let drillData = {
                            r: layerData.diameter / 2,
                            skewing_x:layerData.offset.x,
                            skewing_y:layerData.offset.y,
                            w: layerData.hole.w,
                            h: layerData.hole.h,
                            form: layerData.hole.form,
                            rotate: layerData.hole.rotate
                        }
                        this.data[bigLayer].push(createLayerData(drillData));
                    });
                }
            }
        });

        function createLayerData(data){
            let figureInstance = self.createLayerIns(data);
            let layerData = {
                offset:{
                    x:data.skewing_x,
                    y:data.skewing_y
                },
                rotate: data.rotate,
                figure:figureInstance
            };
            return layerData;
            
        }
    }

    getDrawData(scale){
        let padSmallLayer = ["b","bs_t","bs_b","bp_t","bp_b","t","ts_t","ts_b","tp_t","tp_b","i","is_t","is_b","ip_t","ip_b","drill"];
        let draw = [];
        padSmallLayer.forEach(layer=>{
            let layerData = this.data[layer];
            if(layer !== "drill"){
                if(layerData && layerData.figure){
                    let drawData = getLayerDrawData(layer,layerData);
                    drawData.fillStyle = styleConfig.pin.color[layer];
                    draw.push(drawData);
                }
            }else{
                layerData.forEach(drill=>{
                    let drawData = getLayerDrawData(layer,drill);
                    drawData.fillStyle = styleConfig.pin.color[layer];
                    draw.push(drawData);
                });
            }
        });
        return draw;
        function getLayerDrawData(layer,layerData){
            if(layerData.figure instanceof BasePath){
                let layerDraw = {
                    layer,
                    data:{
                        path:layerData.figure.getDrawData(scale),
                        skewing_x: layerData.offset.x * scale,
                        skewing_y: -layerData.offset.y * scale,
                        rotate: layerData.rotate,
                        form: "PATH"
                    }
                }
                return layerDraw;
            }else{
                let drawData = layerData.figure.getDrawData(scale);
                drawData.skewing_x = layerData.offset.x * scale;
                drawData.skewing_y = - layerData.offset.y * scale;
                drawData.rotate = layerData.rotate;
                let layerDraw = {
                    data:drawData,
                    layer
                }
                return layerDraw;
            }
        }
    }

    getRange(){
        let padLayer = ["b","bs_t","bs_b","bp_t","bp_b","t","ts_t","ts_b","tp_t","tp_b","i","is_t","is_b","ip_t","ip_b"];
        let range = null;
        padLayer.forEach(layer=>{
            if(this.data[layer]){
                range = computedUtil.compareRange(range,this.data[layer].figure.getRange());
            }
        });
        if(this.data.drill){
            this.data.drill.forEach(dirll=>{
                range = computedUtil.compareRange(range,dirll.figure.getRange());
            })
        }
        return range;
    }

    createLayerIns(data){
        let nameConversion = {CIRCLE:"C",CIRCLE_DRILL:"C",SQUARE:"S",RECTANGLE:"S",RECTANGLE_DRILL:"S",SQUARE_DRILL:"S",OCTAGON:"OC",HOLE_OCTAGON:"OC",OCTAGON_DRILL:"OC",OBLONG_Y:"OV",OVAL:"OV",OBLONG_X:"OV",HOLE_OVAL:"OV",OVAL_DRILL:"OV",OVAL_SLOT:"OV"};
        let form = nameConversion[data.form.toUpperCase()] || data.form.toUpperCase();
        if(form == "C"){
            return new BaseCircle(this.units,data);
        }
        if(form == "S"){
            return new BaseRect(this.units,data);
        }
        if(form == "CS"){
            return new BaseRoundRect(this.units,data);
        }
        if(form == "OV"){
            return new BaseOval(this.units,data);
        }
        if(form == "PATH"){
            return new BasePath(this.units,data.path);
        }
        if(form == "POLY"){
            return new BasePolygon(this.units,data);
        }
    }
}

class VPad extends EPad{
    type = "vpad";

    drillMark = "";

    name = "";

    constructor(units,data){
        super(units,data);
    }

    setParent(parent){
        this.parent = parent;
    }

    setRawData(padData){
        super.setRawData.call(this,padData);
        this.name = padData.name;
        this.drillMark = padData.drillMark;
        console.log(this,this.name,this.drillMark,padData);
    }

    getRawData(){
        let rawData = super.getRawData();
        rawData.drillMark = this.drillMark;
        rawData.name = this.name;
        return rawData;
    }
}